package com.rdk.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.rdk.constant.StrConstant;
import com.rdk.service.TUserGroupService;
import com.rdk.utils.BusinessException;
import com.rdk.utils.CommonUtil;


/**
 * 用户组
 * @author wang_lei
 *
 */
@Controller
public class UserGroupAction extends BaseAction{
	
    @Autowired
    private TUserGroupService userGroupService;
	
	/**
	 * 
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "userGroup")
	public ModelAndView addGroup(HttpServletRequest request, ModelMap map)throws BusinessException{
		
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		
		if("0".equals(userId)){
			
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		
		}
		
		Map<String,Object> object = new HashMap<String,Object>();
		
		object.put("resultCode", 1);
		object.put("resultDes", "");
		
		userGroupService.addUserGroup(request,userId);
		object.put("data", userGroupService.findUserGroup(userId));
		
		return new ModelAndView("jsonView", object);
	}
	
	/**
	 * 
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "userGroup/{groupId}")
	public ModelAndView queryGroup(HttpServletRequest request, ModelMap map,@PathVariable String groupId)throws BusinessException{
		
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		
		Map<String,Object> object = new HashMap<String,Object>();
		
		object.put("resultCode", 1);
		object.put("resultDes", "");
		object.put("data", userGroupService.queryUserGroup(groupId, userId));
		
		return new ModelAndView("jsonView", object);
	}
	

	/**
	 * 
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "userGroup/{groupId}/del")
	public ModelAndView deleteGroup(HttpServletRequest request, ModelMap map,@PathVariable String groupId) throws Exception{
		
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		
		Map<String,Object> object = new HashMap<String,Object>();
		object.put("resultCode", 1);
		object.put("resultDes", "");
		userGroupService.removeUserGroup(groupId, userId);
		object.put("data", userGroupService.findUserGroup(userId));
		return new ModelAndView("jsonView", object);
	}
	
	/**
	 * 
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "userGroup/{groupId}/update")
	public ModelAndView updateGroup(HttpServletRequest request, ModelMap map,@PathVariable String groupId)throws Exception{
		
		String userId = CommonUtil.getSession(request, StrConstant.PARAM_USERID);
		if("0".equals(userId)){
			userId = CommonUtil.getRequest(request, StrConstant.PARAM_USERID);
		}
		Map<String,Object> object = new HashMap<String,Object>();
		object.put("resultCode", 1);
		object.put("resultDes", "");
		userGroupService.updateUserGroup(groupId, request,userId);
		object.put("data", userGroupService.findUserGroup(userId));
		return new ModelAndView("jsonView", object);
	}
}
